package controller;

import com.alibaba.fastjson.JSON;
import dao.ManagementDao;
import dao.impl.ManagementDaoImpl;
import entity.Management;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.List;

@WebServlet(urlPatterns = "/managementController")
public class ManagementController extends HttpServlet {
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        String op = req.getParameter("op");
        HttpSession session = req.getSession(true);
        PrintWriter out = resp.getWriter();
        if ("chaxu".equals(op)) {
            String building_name=req.getParameter("building_name");
            String floor=req.getParameter("floor");
            String room_number=req.getParameter("room_number");
            String bed_number = req.getParameter("bed_number");
            ManagementDao dao = new ManagementDaoImpl();
            List<Management> list = null;
            try {
                list = dao.show(building_name, Integer.parseInt(floor),room_number,bed_number);
            } catch (SQLException e) {
                e.printStackTrace();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
            if (list != null) {
                session.setAttribute("list", list);
            }
            out.write(JSON.toJSONString(list));
        }
        if ("xinzen".equals(op)) {
            String bed_number = req.getParameter("bedNumber");
            String room_number = req.getParameter("roomNumber");
            String floor = req.getParameter("floorNumber");
            ManagementDao dao = new ManagementDaoImpl();
            int a = 0;
            try {
                a = dao.addBedAndRoom(bed_number, room_number, Integer.parseInt(floor));
            } catch (SQLException e) {
                e.printStackTrace();
                } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
            if (a > 0) {
                out.write("新增成功");
            } else {
                out.write("新增失败");
            }


        }
        if ("shanchu".equals(op)){
            String bed_id=req.getParameter("bed_id");
            ManagementDao dao=new ManagementDaoImpl();
            int s=0;
            try {
                s=dao.Delete(Integer.parseInt(bed_id));
            } catch (SQLException e) {
                e.printStackTrace();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
            if (s>0){
                out.write("删除成功");
            }else {
                out.write("删除失败");
            }
        }
        if ("loudong".equals(op)){
            ManagementDao dao=new ManagementDaoImpl();
            List<Management>list=null;
            try {
                list=dao.Buildings();
            } catch (SQLException e) {
                e.printStackTrace();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
            out.write(JSON.toJSONString(list));
        }

        if ("loucen".equals(op)){
            ManagementDao dao=new ManagementDaoImpl();
            List<Management>list=null;
            try {
                list= dao.Floor();
            } catch (SQLException e) {
                e.printStackTrace();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
            out.write(JSON.toJSONString(list));
        }
    }

}
